-- Log
-- Create by weism
-- 提供日志、trace的一些功能接口

local MAX_LOG_NUM = 1500;
local logHistory = {};
local outputFunc = nil;

local MAX_TRACEBACK_NUM = 3;
local tracebackList = {};

--红色
R = "\27[31m"

--绿色
G = "\27[32m"

--黄色
Y = "\27[33m"

--蓝色
B = "\27[34m"

--紫色
P = "\27[35m"

--白色
W = "\27[37m"

require "core/mobile/Platform"
if isWindowsPlatform() or IS_VERIFY_CLIENT == 1 then
    sys_print = sys_print or custom_print;
else
    sys_print = sys_print or print;
end

-- 是否允许输出 trace
function is_trace_enable()
    if DEBUG_MODE ~= 1 and RELEASE_LOG_ENABLED ~= 1 and ENABLE_PRINT ~= 1 and
      (not _G["dislist"] or _G["dislist"]["release_log_enabled"] ~= 1) then
        return false;
    end

    return true;
end

-- 重定义print
function print(msg)
    if TEST_FIGHT == 1 then
        return;
    end

    if DEBUG_MODE ~= 1 and ENABLE_PRINT ~= 1 then
        if RELEASE_LOG_ENABLED == 1 or
            (_G["dislist"] ~= nil and _G["dislist"]["release_log_enabled"] == 1) then
            sys_print = release_print;
        else
            do return end
            sys_print = function() end;
        end
    else
        if not isWindowsPlatform() and IS_VERIFY_CLIENT ~= 1 then
            sys_print = release_print;
        end
    end

    local msg = string.format("%s %s", os.date(), tostring(msg));

    sys_print(msg);

    -- 缓存到历史日志记录中，超过最大数删除第一个
    if #logHistory >= MAX_LOG_NUM then
        table.remove(logHistory, 1);
    end

    table.insert(logHistory, msg);

    if outputFunc then
        outputFunc(msg);
    end
end

-- 打印红色字
function printR(msg)
    if isWindowsPlatform() then
        print(R..msg..W);
    else
        print(msg);
    end
end

-- 打印绿色字
function printG(msg)
    if isWindowsPlatform() then
        print(G..msg..W);
    else
        print(msg);
    end
end

-- 普通打印日志
function cclog(value,...)
    if not is_trace_enable() then
        return;
    end

    local a = {...}
    local i = 0
    if(type(value) == "string") then
        value = string.gsub(value,"%%([o,s,d,f])",function(c)
            i = i+1
            if c == "s" then
                return a[i]
            else
                return (watch(a[i]))
            end
        end)
    end

    print(value);
end

-- trace信息
function trace(file, value, ...)
    if not is_trace_enable() then
        return;
    end

    local a = {...}
    local i = 0
    if(type(value) == "string") then
        value = string.gsub(value,"%%([o,s,d,f])",function(c)
            i = i+1
            if c == "s" then
                return a[i]
            else
                return (watch(a[i]))
            end
        end)
    else
        value = "";
    end

    local msg = "[" .. file .. "]" .. value;
    print(msg);
end

-- 获取日志信息
function getLogHistory(from, to)
    if (from == 0 and to == 0)
      or (from > to) then
        return "total: "..#logHistory;
    end

    if from < 1 then
        from = 1;
    end

    local count = #logHistory;
    if from > count then
        from = count - math.abs(to - from);
    end

    if to > count then
        to = count
    end

    local str = {};
    local pos = 0;
    for i = from, to do
        str[pos] = logHistory[i];
        pos = pos + 1;
    end

    return table.concat(str, "\n");
end

-- 设置重定向函数
function setOutputFunc(func)
    outputFunc = func
end

-- 返回最近的N条日志信息
function getLastLogHistory(num)
    local total = #logHistory;
    local start = total - num;
    if start < 1 then
        start = 1;
    end

    local history = {};
    for i = start, total do
        table.insert(history, logHistory[i])
    end

    return history;
end

-- 记录traceback
function recordTraceback(msg)
    cclog(msg)

    if #tracebackList >= MAX_TRACEBACK_NUM then
        table.remove(tracebackList, 1);
    end

    table.insert(tracebackList, msg);
end

-- 获取traceback列表
function getTracebackList()
    return tracebackList;
end

-- 获取错误日志
function getErrorLog()
    local str = "";
    local tracebackList = getTracebackList();
    for _, err in pairs(tracebackList) do
        str = str .. "\n" .. err;
    end
    return str;
end
